package com.ac.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ac.aop.Auth;
import com.ac.model.Cron;
import com.ac.model.Page;
import com.ac.model.query.CronQ;
import com.ac.response.Res;
import com.ac.service.AuthTokenService;
import com.ac.service.CronService;
import com.ac.util.StrUtils;

import lombok.AllArgsConstructor;

@RestController
@RequestMapping("cron")
@AllArgsConstructor
public class CronController {

	private final CronService CronService;
	private final AuthTokenService ts;

	@Auth
	@PostMapping(value = "list")
	public Res list(@RequestHeader("token") String token, @RequestBody CronQ uq) {
		Page page = CronService.list(uq);
		Res res = Res.success(null, page.getList(), page.getTotal());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "save")
	public Res save(@RequestBody Cron cron, @RequestHeader("token") String token) {
		if (StrUtils.isEmpty(cron.getId()))
			CronService.insert(cron);
		else
			CronService.update(cron);
		Res res = Res.success("保存成功");
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "remove")
	public Res remove(@RequestBody Cron api, @RequestHeader("token") String token) {
		CronService.remove(api);
		Res res = Res.success("删除成功");
		return ts.getToken(res, token);
	}

}
